package com.eostoken.eos_wallet.net.common;

import com.blankj.utilcode.util.Utils;
import com.eostoken.eos_wallet.net.intercoptor.BaseUrlInterceptor;
import com.eostoken.eos_wallet.net.intercoptor.CommonParamInterceptor;
import com.eostoken.eos_wallet.net.intercoptor.HttpCacheInterceptor;
import com.eostoken.eos_wallet.net.intercoptor.LoggingInterceptor;
import com.eostoken.eos_wallet.net.ssl.SSLSocketclient;
import com.eostoken.eos_wallet.utils.Constants;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtils {
    private static final String TAG = "RetrofitUtils";

    public static OkHttpClient.Builder getOkHttpClientBuilder() {
        File cacheFile = new File(Utils.getApp().getCacheDir(), "cache");
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
        return new OkHttpClient.Builder()
                .readTimeout(Constants.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
                .connectTimeout(Constants.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
                .addInterceptor(new BaseUrlInterceptor())
                .addInterceptor(new CommonParamInterceptor())
                .addInterceptor(new LoggingInterceptor())
                .addNetworkInterceptor(new HttpCacheInterceptor())
                .cache(cache);


    }

    public static Retrofit.Builder getRetrofitBuilder() {//String baseUrl
        OkHttpClient okHttpClient = getOkHttpClientBuilder().build();
        return new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                .baseUrl(Constants.EXCHANGE_URL_EOS);
    }

}
